﻿using SigFinder.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.Mvc;

namespace SigFinder.MVC.Controllers
{
    public class FileController : Controller
    {
      
        // GET: File
        public ActionResult Index()
        {
            return View();
        }

        // GET: Admin/File/Details/5
        public PartialViewResult PopuFinder()
        {
            return PartialView("_PopuFinder");
        }
        public ActionResult FinderForTinyMCE()
        {
            return View();
        }
        public ActionResult DemoTinyMCE()
        {
            return View();
        }

        public ActionResult FinderForCkeditor()
        {
            return View();
        }

        public ActionResult DemoCkeditor()
        {
            return View();
        }
        // GET: Admin/File/Create
        public PartialViewResult SigFinder()
        {
            return PartialView("_SigFinder");
        }




        [HttpPost]  //CKEditor UploadImage
        public ActionResult UploadImage(HttpPostedFileBase upload, string CKEditorFuncNum, string CKEditor, string langCode)
        {
            string url; // url to return
            string message; // message to display (optional)

            string ImageName = Path.GetFileName(upload.FileName);
            var ext = Path.GetExtension(upload.FileName);

            var uploadDir = Server.MapPath(FileHelper.RootDirectory + "/Images");
            if (!Directory.Exists(uploadDir))
            {
                Directory.CreateDirectory(uploadDir);
            }

            var filePath = FileHelper.RootDirectory + "/Images/" + ImageName;
            string Physicalpath = Server.MapPath(filePath);
            if (System.IO.File.Exists(Physicalpath))
            {
                //Physicalpath.LastIndexOf('.');
                var newPath = filePath.Replace(ext, DateTime.Now.ToFileTimeUtc().ToString() + ext);
                Physicalpath = Server.MapPath(newPath);
                upload.SaveAs(Physicalpath);
                url = newPath;
            }
            else
            {
                upload.SaveAs(Physicalpath);
                url = filePath;
            }

            message = "文件已上传成功！";

            // since it is an ajax request it requires this string
            string output = @"<html><body><script>window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum + ", \"" + url + "\", \"" + message + "\");</script></body></html>";
            return Content(output);
        }
    }
}